module.exports = {

  index: async function (req, res) {
    res.view('users/index');
  },
  loginer:function(req,res){
    res.view('users/loginer');
  },
  ce:function(req,res){
    res.view('users/ce');
  },
  souye:function(req,res){
    res.view('users/souye');
  },
  ge:function(req,res){
    res.view('users/ge');
  },
  gtwo:function(req,res){
    res.view('users/gtwo');
  },
  shangcheng:function(req,res){
    res.view('users/shangcheng');
  },
  regist:function(req,res){
    res.view('users/regist');
  },
  zhuce:function(req,res){
    res.view('users/zhuce');
  },
  denglu:function(req,res){
    res.view('users/denglu');
  },
  detail: async function (req, res) {
    return res.json({
      todo: 'detail() is not implemented yet!'
    });
  },
  register:async function (req, res) {
    let params=req.allParams();
    console.log(params)
    if(params.pwd!=params.pwdd)return res.json(3);
    let row=await User.findOne({username:params.username});
    if(row!=undefined)return res.json(2);
    delete params.repwd;
     row=await User.create(params).fetch()
    // let qq=await User.find({username:'xq'})
    console.log(row)
     req.session.userId={username:row.username,pwd:row.pwd};
     console.log('注册成功')
     res.json(1)
  },
  xiugai:async function (req, res) {
    let params=req.allParams();
    console.log(params.where)
    let row=await User.findOne({username:req.session.userId.username});
    row=await User.update({username:req.session.userId.username},{phone:params.phone,where:params.where,speek:params.speek}).fetch();
    console.log('修改该成果')
    res.json(1)
    console.log('修改该成果')
  },
  gouwu:async function (req, res) {
    let row=await card.find({username:req.session.userId.username});
    res.json({row:row})
  },
  dele:async function (req, res) {
    console.log(req.query['id'])
    let row=await card.destroy({id:req.query['id']});
    res.json(1)
  },
  user_session: function (req, res) {
    if(req.session.userId==undefined)return res.json(false);
    res.json({username:req.session.userId.username,pwd:req.session.userId.pwd})
  },
 login:async function (req, res) {
    let params=req.allParams();
    let row=await User.findOne(params);
    console.log(row)
    if(row==undefined)return res.json(2);
    req.session.userId={username:row.username,pwd:row.pwd};
    res.json(1);
  },
  list:async function (req, res) {
    let qq=await shop.find({})
    console.log(qq)
    res.json({qq:qq})
  },
  logout: async function (req, res) {
    console.log(req.session.userId)
    delete req.session.userId;
    console.log(req.session.userId)
    res.json(true);
  },
  geren: async function (req, res) {
    if(req.session.userId==undefined)return res.json(2)
    res.json(1)
  },
  geen: async function (req, res) {
    let qq=await User.find({username:req.session.userId.username})
    console.log(qq)
    res.json({qq:qq})
  },
  addCart:async function (req, res) {
    let cid=req.query['id'];
    if(req.session.userId==undefined)return res.json(3);
    let username=req.session.userId.username;
    let row1=await User.findOne({username:username});
    let row2=await shop.findOne({id:cid});
    console.log('aaaaaa')
    console.log(row1['where']);
    console.log(row1['username']);
    if(row1['where']=='') return res.json(4);
    let row=await card.findOne({cid:cid,username:username});
    if(row!=undefined)return res.json(2);
    row=await card.create({username:username,cid:cid,num:'1',img:row2.img,money:row2.money,name:row2.name}).fetch()
    // row.cid.push(id);
    // await Normal.update({username:username},{cid:row.cid}).fetch();
    res.json(1);
  },
  xiangqin:async function (req, res) {
    let bb=await shop.findOne({id:req.query['id']})
    // console.log(bb)
    res.view(bb);
  },
  liao: async function (req, res) {
    let bb=req.session.userId
    console.log(bb)
    res.json(bb)
},
};

